<?php

namespace backend\controllers;


use common\models\Active;
use common\models\Complaint;
use common\models\ComplaintImg;
use common\models\CountComplaint;
use common\models\Goods;
use common\models\Order;
use common\models\Shop;
use common\models\User;
use common\service\CommonService;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use common\service\GoodsService;

class ComplaintController extends BaseController
{
    //列表
    public function actionIndex(){
       if($this->isGet() && $this->isAjax()){
           $query = Complaint::find();

           if(!empty($this->get('search_type')) && !empty($this->get('search_value'))){
               if($this->get('search_type') == 1){
                   //按手机号搜索
                   $query->andWhere(['like','user_phone',$this->get('search_value')]);

               }else{
                   //按店铺名称搜索
                   $query->andWhere(['like','user_name',$this->get('search_value')]);
               }
           }
           if(!empty($this->get('status'))){
               $query->andWhere(['status'=>$this->get('status')-1]);
           }

           if(!empty($this->get('start_time'))){
               $query->andWhere(['>=','create_time',strtotime($this->get('start_time'))]);
           }
           if(!empty($this->get('end_time'))){
               $query->andWhere(['<=','create_time',strtotime($this->get('end_time'))]);
           }

           $p = intval( $this->get('page',1) );
           $this->page_size = intval( $this->get('limit',10) );
           $p = ( $p > 0 )? $p : 1;
           $count = $query->count();


           $offset = ($p - 1) * $this->page_size;

           $lists = $query->orderBy(['id' => SORT_DESC])
               ->offset( $offset )
               ->limit( $this->page_size )
               ->asArray()
               ->all();
           if(!empty($lists)){
               $shop_id = array_column($lists,'shop_id');
               $shop = Shop::find()->where(['id'=>$shop_id])->asArray()->all();
               $new_shop = [];
               foreach ($shop as $item){
                   $new_shop[$item['id']] = $item['shop_name'];
               }
               unset($item);
               foreach ($lists as &$list){
                   $list['shop_name'] = '';
                   if(isset($new_shop[$list['shop_id']]) && !empty($new_shop[$list['shop_id']])){
                       $list['shop_name'] = $new_shop[$list['shop_id']];
                   }
               }
           }
           return $this->renderPageJson($lists,$count,ConstantService::$response_tab_success);
       }
        return $this->render('index');
    }

    //查看
    public function actionInfo(){
        $id = $this->get('id');
        $row = Complaint::findOne(['id'=>$id]);
        $row = $row->toArray();
        $images = ComplaintImg::find()->where(['complaint_id'=>$id])->asArray()->all();
        if(!empty($images)){
            foreach ($images as &$image){
                $image['image'] = GlobalUrlService::buildCdnUrl($image['image']);
            }
            unset($image);
        }
        $shop = Shop::findOne(['id'=>$row['shop_id']]);
        $row['image'] = $images;
        $row['shop_name'] = $shop['shop_name'];
        return $this->render('info',['row'=>$row]);
    }



}